package org.beanstech.dao.service;

import java.util.List;

import org.beanstech.util.StaticHibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;




public abstract class AbstractDao {
	
	public int insert(Object obj){
	
		int id = 0;
		try{
			
			Session session = StaticHibernateUtil.getSessionTL();
			StaticHibernateUtil.startTransaction();
			id = (Integer)session.save (obj);
			StaticHibernateUtil.commitTransaction();
			
			
		}catch(Exception e){
			
			e.printStackTrace();
		}
		return id;
	}
	
	
	public void update(Object obj){
		
		
		try{
			
			Session session = StaticHibernateUtil.getSessionTL();
			StaticHibernateUtil.startTransaction();
			session.update (obj);
			StaticHibernateUtil.commitTransaction();
			
			
		}catch(Exception e){
			
			e.printStackTrace();
		}
		
	}
	
	
	public Object getObject(Class<?> classname, int id){
	
		Object myObj = null;
		try{
			
			
			Session session = StaticHibernateUtil.getSessionTL();
			StaticHibernateUtil.startTransaction();
			myObj = session.get(classname, id);
			StaticHibernateUtil.commitTransaction();
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return myObj;
	}
	
	
	
	
	 public List executeHqlQuery(final String hqlQuery) {
		 
		  Session session = StaticHibernateUtil.getSessionTL();
		  StaticHibernateUtil.startTransaction();
		  final Query query = session.createQuery(hqlQuery);
		  List q = query.list();
		  StaticHibernateUtil.commitTransaction();
		  return q;
	    }
	
}
